package com.example.blog.controller;

import com.example.blog.config.page.PageRequest;
import com.example.blog.config.page.PageResult;
import com.example.blog.entity.Notice;
import com.example.blog.service.NoticeService;
import com.example.blog.util.JsonResult;
import com.example.blog.util.PageUtil;
import com.example.blog.util.Result;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @Author: lu
 * @Date: 2023-09-13-21:33
 * @Description:  公告管理
 */
@Api(tags = "通知公告")
@RestController
@RequestMapping("/notice")
public class NoticeController {
    @Autowired
    NoticeService noticeService;

    /**
     * 分页接口查询
     */
    @ApiOperation(value = "分页查询接口")
    @PostMapping("/list")
    public Result<Object> getNoticePage(@RequestBody @Valid PageRequest pageRequest) {
        List<Notice> noticePage = noticeService.getNoticePage(pageRequest);
        PageInfo pageInfo = new PageInfo(noticePage);
        PageResult pageResult = PageUtil.getPageResult(pageRequest, pageInfo);
        return  Result.success(pageResult);
    }

    @ApiOperation(value = "添加公告")
    @PostMapping("/create")
    public Result<Object> noticeCreate(@RequestBody @Valid Notice notice) {
        int isStatus = noticeService.saveNotice(notice);
        if (isStatus == 0) {
            return Result.error("添加公告失败");
        }
        return Result.success(isStatus);
    }

    @ApiOperation(value = "修改公告")
    @PostMapping("/update")
    public Result<Object> noticeUpdate(@RequestBody  Notice notice){
        int notice1 = noticeService.updateNotice(notice);
        if (notice1 == 0){
            return Result.error("修改公告失败");
        }
        System.out.println(notice1);
        return Result.success(notice1);
    }

    @ApiOperation(value = "删除公告")
    @PostMapping("/delete/{id}")
    public Result<Object> noticeDelete(@PathVariable Integer id){
        noticeService.deleteNotice(id);
        return Result.success();
    }

}
